package com.flow.asyncflow.core.utils.configuration;

import lombok.Data;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;


@Data
public class AsyncFlowConfiguration {

    //日志保存的MongoDB链接字符串
    private String mongoDBConfigStr;
    //日志保存的MongoDB库名
    private String mongoDBName;
    //日志保存的MongoDB表名
    private String mongoDBTableName;
    //当前所处环境
    private String evn;
    //发送报警的接口地址
    private String nodeArmUrl;
    //发送业务报警的接口地址
    private String businessArmUrl;

    //项目标识
    private  String appUK;
    //保存业务日志地址
    private  String businessLogUrl;
    //数据库信息（数据库名称，数据库SqlSessionFactory名称）
    private Map<String, String> dataBaseInfo = new HashMap<>();


    public AsyncFlowConfiguration() {
        init();
    }

    private void init() {
        Yaml yaml = new Yaml();
        InputStream resourceAsStream = AsyncFlowConfiguration.class.getClassLoader().getResourceAsStream("asyncflow.yml");
        Map obj = (Map) yaml.load(resourceAsStream);
        for (Object key : obj.keySet()) {

            if (key.equals("mongoDBConfigStr")) {
                this.mongoDBConfigStr = (String) obj.get(key);
                continue;
            }
            if (key.equals("mongoDBName")) {
                this.mongoDBName = (String) obj.get(key);
                continue;
            }
            if (key.equals("mongoDBTableName")) {
                this.mongoDBTableName = (String) obj.get(key);
                continue;
            }
            if (key.equals("businessLogUrl")) {
                this.businessLogUrl = (String) obj.get(key);
                continue;
            }
            if (key.equals("nodeArmUrl")) {
                this.nodeArmUrl = (String) obj.get(key);
                continue;
            }
            if (key.equals("businessArmUrl")) {
                this.businessArmUrl = (String) obj.get(key);
                continue;
            }
            if (key.equals("evn")) {
                this.evn = (String) obj.get(key);
                continue;
            }
            if (key.equals("appUK")) {
                this.appUK = (String) obj.get(key);
                continue;
            }
            String keyStr = (String) key;
            if (keyStr.startsWith("db-")) {
                String value = (String) obj.get(key);
                this.dataBaseInfo.put(keyStr.replace("db-", ""), value);
            }
        }

    }

}
